package com.example.oop.Service;


import com.example.oop.ChatRoom.ChatRoomManager;
import com.example.oop.POJO.Chat;
import com.example.oop.Utils.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class ChatService {
    public org.json.JSONObject doSend(JSONObject args) throws JSONException {
        org.json.JSONObject response;  //定义用来响应的JSON

        ChatRoomManager.i.sendMessage(args);

        response = Response.getJsonObject("0", "信息发送成功！");

        return response;
    }

    public JSONObject doGet(JSONObject args) throws JSONException {
        JSONObject response;  //定义用来响应的JSON
        JSONArray data = new JSONArray();

        if (args != null) {
            List<Chat> chatList = ChatRoomManager.i.getMessage(args);
            if(chatList!=null){
                for (Chat chat : chatList) {
                    JSONObject json = new JSONObject();
                    json.put("from", chat.getSendChatRoom().getUsername());
                    json.put("time", chat.getChatTime());
                    json.put("content", chat.getContent());
                    data.put(json);
                }
            }
        }

        response = Response.getJsonObject("0", "信息获取成功！", data);

        return response;
    }
}
